package com.blacktry.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blacktry.entity.easypoidata.CourseListener;
import com.blacktry.entity.enums.OprLogConst;
import com.blacktry.entity.model.ApiResult;
import com.blacktry.entity.model.Course;
import com.blacktry.entity.model.request.CourseRequest;
import com.blacktry.service.CourseService;
import com.blacktry.utils.ApiResultHandler;
import com.blacktry.utils.log.OperLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author blackTry
 * @since 2021-12-05
 */
@RestController
@RequestMapping("/course")
@Api(value = "课程管理模块", tags = "课程管理接口")
public class CourseController {
    @Autowired
    private CourseService courseService;

    // 上传预计划模板并放入数据库注意文件的数据是否完善
    @OperLog(operModul = "课程管理-课程信息导入", operType = OprLogConst.ADD, operDesc = "课程信息导入")
    @ApiOperation("课程信息导入")
    @PostMapping("/importCourse")
    public ApiResult<String> exam(MultipartFile file) {
        // 这里默认读取第一个sheet
        try {
//            EasyExcel.read(file.getInputStream(), Course .class, new CourseListener()).sheet().headRowNumber(4).doRead();
//            EasyExcel.read(file.getInputStream(), Course.class, new CourseListener()).doReadAll();
            ExcelReader excelReader = EasyExcel.read(file.getInputStream()).build();
            ReadSheet readSheet =
                    EasyExcel.readSheet(0).headRowNumber(4).head(Course.class).registerReadListener(new CourseListener()).build();
            excelReader.read(readSheet);
            // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
            excelReader.finish();
        } catch (ExcelAnalysisException | IOException e) {

        }
        courseService.flushClear();
        return ApiResultHandler.buildApiResult(200, "成功");
    }

    @OperLog(operModul = "课程管理-根据课程id查询数据", operType = OprLogConst.SELECT, operDesc = "根据课程id查询数据")
    @ApiOperation("根据课程id查询数据")
    @GetMapping("/select/{id}")
    public ApiResult<List<Course>> select(@PathVariable Integer id) {
        return ApiResultHandler.buildApiResult(200, "插入成功", courseService.select(id));
    }

    @OperLog(operModul = "课程管理-查询所有数据", operType = OprLogConst.SELECT, operDesc = "查询所有数据")
    @ApiOperation("查询所有数据")
    @GetMapping("/select")
    public ApiResult<List<Course>> selectExam() {
        return ApiResultHandler.buildApiResult(200, "成功", courseService.select());
    }

    @OperLog(operModul = "课程管理-分页查看所有数据", operType = OprLogConst.SELECT, operDesc = "分页查看所有数据")
    @ApiOperation("分页查看所有数据")
    @PostMapping("/select/{page}/{size}")
    public ApiResult<Page<Course>> findAll(@RequestBody Course course, @DefaultValue("1") @PathVariable("page") Integer page, @DefaultValue("6") @PathVariable("size") Integer size) {
        System.out.println("分页查询所有课程数据");
        Page<Course> coursePage = new Page<>(page, size);
        return ApiResultHandler.buildApiResult(200, "请求成功！", courseService.selectPage(course, coursePage));
    }

    @OperLog(operModul = "课程管理-分页查看课程信息", operType = OprLogConst.SELECT, operDesc = "分页查看课程信息")
    @ApiOperation("分页查看课程信息")
    @PostMapping("/selectCourse/{page}/{size}")
    public ApiResult<Page<Course>> selectCourse(@RequestBody CourseRequest courseRequest, @DefaultValue("1") @PathVariable("page") Integer page, @DefaultValue("6") @PathVariable("size") Integer size) {
        System.out.println("分页查询课程信息");
        Page<Course> coursePage = new Page<>(page, size);
        return ApiResultHandler.buildApiResult(200, "请求成功！", courseService.selectCourse(courseRequest, coursePage));
    }

}

